import lparser
from itertools import izip 

class Canvas(object):
	def __init__(self, size):
		self.size = size 
		self.canvas = self.init_canvas()

	def init_canvas(self):
		return [['.' for j in range(self.size)]
			for i in range(self.size)]

	def blit(self, cursor):
		if cursor.x > self.size:
			throwBoundsError('x')
		if cursor.y > self.size:
			throwBoundsError('y')
		# print cursor.x, cursor.y
		self.canvas[cursor.y][cursor.x] = cursor.char

	def draw(self):
		print self

	def clear(self):
		init_canvas(self.size)

	def throwBoundsError(self, coordinate):
		raise 'OutOfBoundsError: %s coordinate outside of Canvas' % coordinate
		
	def __str__(self):
		return '\n'.join([''.join(i) for i in self.canvas])

class Turtle(object):
	def __init__(self, canvas_size, char='X'):
		self.canvas = Canvas(canvas_size)
		self.x = canvas_size / 2
		self.y = canvas_size / 2
		self.char = char
		self.heading = Heading()
		self.canvas.blit(self)

	def move_forward(self, distance):
		for i in range(distance):
			self.x += self.heading.x
			self.y += self.heading.y
			self.canvas.blit(self)

	def move_backward(self, distance):
		for i in range(distance):
			self.x -= self.heading.x
			self.y -= self.heading.y
			self.canvas.blit(self)

	def rotate(self, degrees):
		self.heading.rotate(degrees)


class Heading(object):
	def __init__(self):
		self.direction_values = [
			(0,-1), # 0 (up) 
			(1,-1), # 45
			(1, 0), # 90
			(1, 1), # 135
			(0, 1), # 180
			(-1,1), # 225
			(-1,0), # 270
			(-1,-1), # 315
		]
		self.headings = CircularLinkedThingy(self.direction_values)

	@property
	def current(self):
	    return self.headings.current
	
	@property
	def x(self):
	    return self.headings.current.element[0]

	@property
	def y(self):
	    return self.headings.current.element[1]

	def rotate(self, degrees):
		for i in range(abs(degrees/45)):
			self.headings.decrement() if degrees < 0 else self.headings.increment()


class CircularLinkedThingy(object):
	def __init__(self, elms):
		self.head = Node(None)
		self.tail = Node(None)
		self.current = None
		for elm in elms: 
			self.add(elm)

	def add(self, element):
		if self.head.element == None: 
			self.head = Node(element)
			self.tail = self.head
			self.head.prev = self.tail
			self.tail.next = self.head
			self.current = self.head
		
		else:
			self.tail.next = Node(element)
			self.tail.next.prev = self.tail
			self.tail = self.tail.next

			self.tail.next = self.head 
			self.head.prev = self.tail

	def increment(self):
		self.current = self.current.next
		return self.current.element

	def decrement(self):
		self.current = self.current.prev
		return self.current.element

class Node(object):
	def __init__(self, element, prev=None, next=None):
		self.element = element
		self.prev = prev 
		self.next = next 


def pairwise(iterable):
    a = iter(iterable)
    return izip(a, a)

if __name__ == '__main__': 
	command_list = lparser.parse(lparser.load('complex.logo'))

	canvas_size = int(command_list[0])
	turtle = Turtle(canvas_size)

	for cmd, val in pairwise(command_list[1:]):
		val = int(val)
		if cmd == 'FD': 
			turtle.move_forward(val)
		elif cmd == 'RT':
			turtle.rotate(val)
		elif cmd == 'LT':
			turtle.rotate(-val)
		elif cmd == 'BK':
			turtle.move_backward(val)

	turtle.canvas.draw()


